from django.shortcuts import render
from rest_framework import viewsets
# Create your views here.
from rest_framework.permissions import IsAuthenticated, AllowAny

from goods.models import Goods,SeckillGoods
from .serializers import GoodsListSerializer,GoodsDetailSerializer


class GoodsView(viewsets.ReadOnlyModelViewSet):
    queryset = SeckillGoods.objects.all()
    permission_classes = [AllowAny]

    # def get_permissions(self):
    #     if self.action == 'list':
    #         return [AllowAny()]
    #     elif self.action == 'retrieve':
    #         return [IsAuthenticated()]
    #
    #     return [IsAuthenticated()]

    def get_serializer_class(self):
        if self.action == 'list':
            return GoodsListSerializer
        elif self.action == 'retrieve':
            return GoodsDetailSerializer

        return GoodsListSerializer

    def list(self, request, *args, **kwargs):
        response = super().list(request,*args,**kwargs)
        return render(request,'goodsList.html',{'goodsList':response.data})

    def retrieve(self, request, *args, **kwargs):
        response = super().retrieve(request,*args,**kwargs)
        # print(response.data)
        return render(request,'goodsDetail.html',{'goods':response.data})